package dev.common.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import dev.common.mapper.BaseMapper;
import dev.common.mapper.common.Mapper;
import dev.common.mapper.entity.Example;
import dev.common.service.IService;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.List;

/**
 * @program: JoyDev
 * @description: 业务基础类
 * @author: MR.C
 * @create: 2018-02-07 16:34
 **/
public class BaseService<T> implements IService<T>{

    private  Class<T> clazz;

    @Autowired
    BaseMapper<T> mapper;

    @SuppressWarnings("unchecked")
    public BaseService() {
        Type genType = getClass().getGenericSuperclass();
        Type[] params = ((ParameterizedType) genType).getActualTypeArguments();
        clazz = (Class<T>) params[0];
    }

    protected Class<T> getClazz(){
        return this.clazz;
    }

    @Override
    public List<T> findByExample(Example example) {
        return mapper.selectByExample(example);
    }

    @Override
    public int findCountByExample(Example example) {
        return mapper.selectCountByExample(example);
    }

    @Override
    public int updateByExample(T record, Example example) {
        return mapper.updateByExample(record,example);
    }

    @Override
    public int updateByExampleSelective(T record, Example example) {
        return mapper.updateByExampleSelective(record, example);
    }

    @Override
    public int deleteByExample(Example example) {
        return mapper.deleteByExample(example);
    }

    @Override
    public T findByPrimaryKey(Object key) {
        return mapper.selectByPrimaryKey(key);
    }

    @Override
    public T findOne(T record) {
        return mapper.selectOne(record);
    }

    @Override
    public List<T> find(T record) {
        return mapper.select(record);
    }

    @Override
    public List<T> findAll() {
        return mapper.selectAll();
    }

    @Override
    public int findCount(T record) {
        return mapper.selectCount(record);
    }

    @Override
    public int insert(T record) {
        return mapper.insert(record);
    }

    @Override
    public int insertSelective(T record) {
        return mapper.insertSelective(record);
    }

    @Override
    public int updateByPrimaryKey(T record) {
        return mapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateByPrimaryKeySelective(T record) {
        return mapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(T record) {
        return mapper.delete(record);
    }

    @Override
    public int deleteByPrimaryKey(Object key) {
        return mapper.deleteByPrimaryKey(key);
    }

    @Override
    public List<T> findByRowBounds(T record, int pageNum,int pageSize) {
        RowBounds rowBounds = new RowBounds(pageNum,pageSize);
        return mapper.selectByRowBounds(record, rowBounds);
    }

    @Override
    public List<T> findByExampleAndRowBounds(Example example, int pageNum,int pageSize) {
        RowBounds rowBounds = new RowBounds(pageNum,pageSize);
        return mapper.selectByExampleAndRowBounds(example, rowBounds);
    }

    @Override
    public PageInfo<T> findByEntity(T record, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<T> ts = mapper.select(record);
        return new PageInfo<>(ts);
    }

    @Override
    public PageInfo<T> findByExample(Example example, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<T> ts = mapper.selectByExample(example);
        return new PageInfo<>(ts);
    }

    @Override
    public int insertList(List<T> recordList) {
        return mapper.insertList(recordList);
    }

    @Override
    public int insertUseGeneratedKeys(T record) {
        return mapper.insertUseGeneratedKeys(record);
    }

    @Override
    public List<T> selectByIds(String ids) {
        return mapper.selectByIds(ids);
    }

    @Override
    public int deleteByIds(String ids) {
        return mapper.deleteByIds(ids);
    }
}
